
#include "model/ServerNode.h"
#include "protocol/transport.pb.h"

namespace zeroplus::mq
{
    bool ServerNode::Run()
    {
        auto message = RecvRequest();
        return SendResponse(message);
    }

    bool ServerNode::SendResponse(const Bytes &message)
    {
        zmqpp::message_t zmqMessage;
        zmqMessage << identity_.to_string() << message.to_string();
        auto size = conn_->WriteMsg(zmqMessage);
        return size;
    }

    Bytes ServerNode::RecvRequest()
    {
        auto [zmqMessage, res] = conn_->ReadMsg();
        std::string identity;
        std::string message;
        zmqMessage >> identity >> message;
        identity_ = Bytes::from_string(identity);
        return Bytes::from_string(message);
    }
}
